/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GamePlay;

import ClassFrame.Map;
import com.nokia.mid.payment.IAPClientPaymentException;
import com.nokia.mid.payment.IAPClientPaymentListener;
import com.nokia.mid.payment.IAPClientPaymentManager;
import com.nokia.mid.payment.IAPClientProductData;
import com.nokia.mid.payment.IAPClientUserAndDeviceData;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

/**
 *
 * @author QuyetNM1
 */
public class MIDletGame extends MIDlet implements IAPClientPaymentListener {

    public static final String PURCHASE_ID = "903620";
    public static String PRICE = "0";
    public static boolean purchaseStatus = false;
    private static CanvasGame canvas;
    private static Display display;
//    private Tracker tracker;
    private static IAPClientPaymentManager pManager;

    public MIDletGame() {
        canvas = new CanvasGame(this);
//        tracker = Tracker.getInstance(this, "UA-30587251-1");
//        tracker.addToQueue(new PageView("DuoiHinhBatChu"));
//        tracker.flush(true);

    }

    public void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(canvas);
        pManager = getIAPManager();
        pManager.setIAPClientPaymentListener(this);
        if (Map.levelMax < 19) {
            pManager.getProductData(PURCHASE_ID);
        }
        int status = pManager.getRestorableProducts(IAPClientPaymentManager.ONLY_IN_SILENT_AUTHENTICATION);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
        exit();
    }

    public void exit() {
        canvas = null;
        notifyDestroyed();
    }

    public static Display getDisplay() {
        return display;
    }

    public static boolean purchaseNextLevel() {
        int status = pManager.purchaseProduct(PURCHASE_ID,
                IAPClientPaymentManager.FORCED_AUTOMATIC_RESTORATION);
        if (status != IAPClientPaymentManager.SUCCESS) {
            showAlertMessage(display, "Thanh toán thất bại", "Thanh toán thất bại."
                    + Messages.getPaymentError(status), AlertType.ERROR);
            return false;
        }
        return true;
    }

    public static boolean restoreNextLevel() {
        int status = pManager.restoreProduct(PURCHASE_ID,
                IAPClientPaymentManager.ONLY_IN_SILENT_AUTHENTICATION);
        if (status != IAPClientPaymentManager.SUCCESS) {
            showAlertMessage(display, "Phục hồi thất bại", "Phục hồi thất bại."
                    + Messages.getPaymentError(status), AlertType.ERROR);
            return false;
        }
        return true;
    }

    // Functions necessary to implement IAPClientPaymentListener interface
    public void userAndDeviceDataReceived(int status, IAPClientUserAndDeviceData ud) {
    }

    public void restorableProductsReceived(int status, IAPClientProductData[] productDataList) {
    }

    public void productDataListReceived(int status, IAPClientProductData[] productDataList) {
    }

    public void productDataReceived(int status, IAPClientProductData pd) {
        if (status == OK) {
            PRICE = pd.getPrice();
        }
    }

    public void purchaseCompleted(int status, String purchaseTicket) {
        if (status == OK) {
//            tracker.addToQueue(new PageView("DuoiHinhBatChu_PurchaseComplete " + PRICE));
//            tracker.flush(true);
            purchaseStatus = true;
            Map.levelMax += 3;
            canvas.getRMS().open();
            canvas.getRMS().saveLevelMax(Map.levelMax);
            canvas.getRMS().close();
//            canvas.getMap().initLevelMap();
        } else {
            showAlertMessage("Thanh toán thất bại", "Thanh toán thất bại."
                    + Messages.getPaymentError(status), AlertType.ERROR);
        }
    }

    public void restorationCompleted(int status, String purchaseTicket) {
        if (status == OK) {
//            tracker.addToQueue(new PageView("DuoiHinhBatChu_PurchaseComplete " + PRICE));
//            tracker.flush(true);
            purchaseStatus = true;
            Map.levelMax += 3;
            canvas.getRMS().open();
            canvas.getRMS().saveLevelMax(Map.levelMax);
            canvas.getRMS().close();
//            canvas.getMap().initLevelMap();
        } else {
            showAlertMessage("Phục hồi thất bại", "Phục hồi thất bại."
                    + Messages.getPaymentError(status), AlertType.ERROR);
        }
    }

    public static IAPClientPaymentManager getIAPManager() {
        if (pManager == null) {
            try {
                pManager = IAPClientPaymentManager.getIAPClientPaymentManager();
            } catch (IAPClientPaymentException ipe) {
            }
        }
        return pManager;
    }

    public void showAlertMessage(String title, String alertText, AlertType type) {
        showAlertMessage(display, title, alertText, type);
    }

    public static void showAlertMessage(Display display, String title,
            String alertText, AlertType type) {
        Alert alert = new Alert(title, alertText, null, type);
        display.setCurrent(alert, display.getCurrent());
    }
//    public Tracker getTracker() {
//        return tracker;
//    }
//
//    public void setTracker(Tracker tracker) {
//        this.tracker = tracker;
//    }
}
